package ${package.Controller};

import ${package.Entity}.${entity};
<#if qoConfig??>
import ${qoConfig.packageName}.${qoConfig.className};
</#if>
<#if voConfig??>
import ${voConfig.packageName}.${voConfig.className};
</#if>
import ${package.Service}.${table.serviceName};
import ${superControllerClassPackage};
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@RestController
@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
@Tag(name = "${table.comment!}管理", description = "${table.comment!}相关操作")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    @Autowired
    private ${table.serviceName} ${table.serviceName?uncap_first};

    @GetMapping
    @Operation(summary = "获取${table.comment!}列表", description = "获取所有${table.comment!}记录")
    public ResponseEntity<List<<#if voConfig??>${voConfig.className}<#else>${entity}</#if>>> list() {
        List<${entity}> entities = ${table.serviceName?uncap_first}.list();
<#if voConfig??>
        List<${voConfig.className}> vos = entities.stream().map(entity -> {
            ${voConfig.className} vo = new ${voConfig.className}();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        }).collect(Collectors.toList());
        return ResponseEntity.ok(vos);
<#else>
        return ResponseEntity.ok(entities);
</#if>
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取${table.comment!}", description = "根据ID获取${table.comment!}详细信息")
    public ResponseEntity<<#if voConfig??>${voConfig.className}<#else>${entity}</#if>> getById(
            @Parameter(description = "${table.comment!}ID") @PathVariable Long id) {
        ${entity} entity = ${table.serviceName?uncap_first}.getById(id);
        if (entity == null) {
            return ResponseEntity.notFound().build();
        }
<#if voConfig??>
        ${voConfig.className} vo = new ${voConfig.className}();
        BeanUtils.copyProperties(entity, vo);
        return ResponseEntity.ok(vo);
<#else>
        return ResponseEntity.ok(entity);
</#if>
    }

    @PostMapping
    @Operation(summary = "创建${table.comment!}", description = "创建新的${table.comment!}记录")
    public ResponseEntity<<#if voConfig??>${voConfig.className}<#else>${entity}</#if>> create(
            @Valid @RequestBody <#if qoConfig??>${qoConfig.className}<#else>${entity}</#if> <#if qoConfig??>qo<#else>entity</#if>) {
<#if qoConfig??>
        ${entity} entity = new ${entity}();
        BeanUtils.copyProperties(qo, entity);
</#if>
        boolean saved = ${table.serviceName?uncap_first}.save(entity);
        if (saved) {
<#if voConfig??>
            ${voConfig.className} vo = new ${voConfig.className}();
            BeanUtils.copyProperties(entity, vo);
            return ResponseEntity.status(HttpStatus.CREATED).body(vo);
<#else>
            return ResponseEntity.status(HttpStatus.CREATED).body(entity);
</#if>
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新${table.comment!}", description = "更新现有${table.comment!}记录")
    public ResponseEntity<<#if voConfig??>${voConfig.className}<#else>${entity}</#if>> update(
            @Parameter(description = "${table.comment!}ID") @PathVariable Long id,
            @Valid @RequestBody <#if qoConfig??>${qoConfig.className}<#else>${entity}</#if> <#if qoConfig??>qo<#else>entity</#if>) {
<#if qoConfig??>
        ${entity} entity = new ${entity}();
        BeanUtils.copyProperties(qo, entity);
</#if>
        entity.setId(id);
        boolean updated = ${table.serviceName?uncap_first}.updateById(entity);
        if (updated) {
<#if voConfig??>
            ${voConfig.className} vo = new ${voConfig.className}();
            BeanUtils.copyProperties(entity, vo);
            return ResponseEntity.ok(vo);
<#else>
            return ResponseEntity.ok(entity);
</#if>
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除${table.comment!}", description = "删除${table.comment!}记录")
    public ResponseEntity<Void> delete(
            @Parameter(description = "${table.comment!}ID") @PathVariable Long id) {
        boolean deleted = ${table.serviceName?uncap_first}.removeById(id);
        if (deleted) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
</#if>